<?php
/**
 * Created by liuniukeji.com
 * User: 用户积分
 * Date: 16-3-31
 * @author:zhaojiping QQ: 17620286
 */

namespace Home\Model;
use Think\Model;

class YunshangUserCashAccountModel extends Model{
//    protected $dbName = 'TestYszj';
	protected $dbName = 'YuShangZhiJia';
    protected $tablePrefix = 'dbo.';
    protected $tableName = 'UserCashRedAccount';
    /**
     * 根据手机号查取账户余额
     * 
     */
	  public function getCashAccount1($username){
        $getCash=$this->where("userAccount='".$username."'")->field('extra,TotalCash')->find();
        if(!$getCash){
            $array=array(
                'userAccount'=>$username,
                'TotalCash'=>0,
                'UsedCash'=>0,
                'Createtime'=>date('Y-m-d H:i:s'),
                'IsLocked'=>False,
                'HasLockedCash'=>0,
                'extra'=>0,
            );
           $this->data($array)->add();
           $account=0;
        }else{
            // $money=$getCash['totalcash']-$getCash['usedcash'];
			$money=$getCash['extra'];
            $account=(float)$money;
			$accounts=sprintf("%.2f", $account);
            $point=explode('.',$money);
            if(empty($point[1]) || $point[1]=0 ||$point[1]==00){
                $account=$point[0].'.00';
            }
			if($accounts=='0.'){
                $account='0.'.$account;
            }
         //  $account=empty(explode('.',$getCash['totalcash'])[0])?('0.'.explode('.',$getCash['totalcash'])[1]):$getCash['totalcash'];
        }
        return $account;
    }
    public function getCashAccount($username){
        $getCash=$this->where("userAccount='".$username."'")->field('extra,TotalCash')->find();
        if(!$getCash){
            $array=array(
                'userAccount'=>$username,
                'TotalCash'=>0,
                'UsedCash'=>0,
                'Createtime'=>date('Y-m-d H:i:s'),
                'IsLocked'=>False,
                'HasLockedCash'=>0,
                'extra'=>0,
            );
           $this->data($array)->add();
		   echo $this->getLastSql();
           $account=0;
        }else{
            // $money=$getCash['totalcash']-$getCash['usedcash'];
			$money=$getCash['extra'];
			//$money = number_format($money-0.005,2);
            $account=(float)$money;
			$accounts=sprintf("%.2f", $account);
			if($accounts=='0.'){
                $account='0.'.$account;
            }else{
				$accounta=explode('.', $account);
				$substr=substr($accounta[1],0,2);
				$account=$accounta[0].'.'.$substr;
			}
         //  $account=empty(explode('.',$getCash['totalcash'])[0])?('0.'.explode('.',$getCash['totalcash'])[1]):$getCash['totalcash'];
        }
        return $account;
    }
    /**
     * 发红包减少余额
     * @param username account
     * @return 
     */
    public function reduce_cash_account($username,$account){
        $this->isAccount($username);
	    $this->startTrans();
        if($this->where("userAccount='".$username."'")->setInc('UsedCash',$account)  ){
             $this->commit();
        }else{
             $this->rollback();
        }
        if($this->where("userAccount='".$username."'")->setDec('extra',$account)){
            $this->commit();
         }else{
            $this->rollback();
         }   
		$this->where("userAccount='".$username."'")->save(array('CreateTime'=>date('Y-m-d H:i:s')));
        return true;
    }
    /**
     * 收到红包增加余额
     * @param username account
     * @return 
     */
    public function add_cash_account($username,$account,$i=0){
        $this->isAccount($username);
        //$this->where("userAccount='".$username."'")->setInc('extra',$account);
        //$this->where("userAccount='".$username."'")->setInc('TotalCash',$account);
		$this->startTrans();
        if($this->where("userAccount='".$username."'")->setInc('extra',$account)){
             $this->commit();
         }else{
            $this->rollback();
         }

        if($this->where("userAccount='".$username."'")->setInc('TotalCash',$account)){
             $this->commit();
         }else{
            $this->rollback();
         }

		$this->where("userAccount='".$username."'")->save(array('CreateTime'=>date('Y-m-d H:i:s')));
        return true;
    }
    /**
     * 查询该账户是否存在 不存在增加一条新的数据
     * @param 
     * @return
     */
    public function isAccount($username){
        //$getCash=$this->where("userAccount='".$username."'")->find();
		$sql="SELECT userAccount FROM [YuShangZhiJia].[dbo].[UserCashRedAccount] With(RowLock,UpdLock) WHERE userAccount='".$username."'";
		$getCash=$this->query($sql);
        if(!$getCash){
            $array=array(
                'userAccount'=>$username,
                'TotalCash'=>0,
                'UsedCash'=>0,
                'Createtime'=>date('Y-m-d H:i:s'),
                'IsLocked'=>False,
                'HasLockedCash'=>0,
                'extra'=>0,
            );
           $this->data($array)->add();
        }
        return true;
    }
}
